package com.example.speedwaymanager;

import android.annotation.SuppressLint;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Locale;

@SuppressLint("DefaultLocale")
public class CTeam {

	private ArrayList<CRider> _riders = new ArrayList<CRider>();
	private ArrayList<CRider> _seniorRiders = new ArrayList<CRider>();
	private ArrayList<CRider> _juniorRiders = new ArrayList<CRider>();

	private Integer _points;
	private String _name;
	private Boolean _homeTeam;
	private ArrayList<CRider> _squad = new ArrayList<CRider>();
	private CCoach _coach;
	private boolean _isUser = false;

	/**
	 * constructs a team with name REMARK: to add riders call addRiderToTeam()
	 * 
	 * @param name
	 */
	public CTeam(String name) {
		_name = name;
		_coach = new CCoach(this);
	}

	@Override
	public String toString()
	{
		return _name;
	}
	
	public void addRiderToTeam(CRider rider) {
		_riders.add(rider);
		if (rider.get_type() == ERiderType.eJunior) {
			_juniorRiders.add(rider);
		} else {
			_seniorRiders.add(rider);
		}
	}

	public void clearAllMatchPoints() {
		for (CRider rider : _riders) {
			rider.clearPoints();
		}
		_points = 0;
	}

	public int calculateTeamPoints() {
		_points = 0;
		for (CRider rider : _squad) {
			_points += rider.get_matchStats().get_points();
		}
		return _points;
	}
	
	/**
	 * retrieve rider with given number during match
	 * @return
	 */
	public CRider getRiderWithNumber(int number)
	{
		CRider rider = null;
		for (CRider r:_squad) {		
			if ( r.get_number() == number)
			{
				rider = r;
			}
		}
		return rider;
	}

	/**
	 * @return the _name
	 */
	public String get_name() {
		return _name;
	}
	
	@SuppressLint("NewApi")
	public static String decompose(String s) {
	    return java.text.Normalizer.normalize(s, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","");
	}
	
	/**
	 * @return the resource id name without polish signs and spaces
	 */
	@SuppressLint("DefaultLocale")
	public String get_resourceName() {
		String lower = _name.toLowerCase(Locale.ENGLISH);
		lower = lower.substring(lower.lastIndexOf(" ") + 1, lower.length());
		//lower = lower.replaceAll(" ", "");
		lower = decompose(lower);
		return lower;
	}

	/**
	 * @return the _riders
	 */
	public ArrayList<CRider> get_riders() {
		return _riders;
	}

	/**
	 * @return the _points
	 */
	public int get_points() {
		return _points;
	}

	/**
	 * @return the _squad
	 */
	public ArrayList<CRider> get_squad() {
		return _squad;
	}

	/**
	 * @return the _homeTeam
	 */
	public Boolean get_homeTeam() {
		return _homeTeam;
	}

	/**
	 * @param _homeTeam
	 *            the _homeTeam to set
	 */
	public void set_homeTeam(Boolean _homeTeam) {
		this._homeTeam = _homeTeam;
	}

	/**
	 * @return the _coach
	 */
	public CCoach get_coach() {
		return _coach;
	}

	/**
	 * @return the _seniorRiders
	 */
	public ArrayList<CRider> get_seniorRiders() {
		return _seniorRiders;
	}

	/**
	 * @return the _juniorRiders
	 */
	public ArrayList<CRider> get_juniorRiders() {
		return _juniorRiders;
	}

	/**
	 * @return the _isUser
	 */
	public boolean is_isUser() {
		return _isUser;
	}

	/**
	 * @param _isUser the _isUser to set
	 */
	public void set_isUser(boolean _isUser) {
		this._isUser = _isUser;
	}

	/**
	 * @param _squad the _squad to set
	 */
	public void set_squad(ArrayList<CRider> _squad) {
		this._squad = _squad;
	}
}

final class ridersAvgHomeComparator implements Comparator<CRider> {
	@Override
	public int compare(CRider r1, CRider r2) {
		return r1.get_avgHome().compareTo(r2.get_avgHome());
	}
}

final class ridersAvgAwayComparator implements Comparator<CRider> {
	@Override
	public int compare(CRider r1, CRider r2) {
		return r1.get_avgAway().compareTo(r2.get_avgAway());
	}
}